﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MJM.RPP.EntidadesNegocio;
using MJM.SPP.EntidadesNegocio;
using MJM.Principal;

namespace wRegPadron.frmOpciones
{
    public partial class ucReportDocumentos : System.Web.UI.UserControl
    {
        protected static List<DocsAreaEN> lstDocsAreaEN = new List<DocsAreaEN>();
        protected static List<DocumentosEN> lstDocumentosEN = new List<DocumentosEN>();
        protected static String FecIni, FecFin;
        protected static int ID;

        private void AbreVentanaForm(string ventana)
        {
            string Clientscript = "<script>open('" + ventana + "','Externo','width=850,height=600,status=no,resizable=no,scrollbars=yes')</script>";
            //string Clientscript = "<script>open('" + ventana + "')</script>";
            if (!Page.ClientScript.IsStartupScriptRegistered("WOpen"))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "WOpen", Clientscript);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SubOpcionEN objSubOpcionEN = MediadorCN.GetSubOpcion().ObtenerTexto("MenuReportDocumentos");
                lblTitulo.Text = objSubOpcionEN.SUBOPT_chNOMSOP;
                lblDescripcion.Text = objSubOpcionEN.SUBOPT_chDESSOP;
                bdpFecFin.SelectedDate = DateTime.Now.Date;
                bdpFecInicio.SelectedDate = DateTime.Now.Date;
                //gvAreaDocu.Rows[0].FindControl("gv");
                FecFin = ""; FecIni = "";
                ID = 0;
            }
        }

        protected void btnAceptar_Click(object sender, EventArgs e)
        {
            List<AreaEN> lst = MediadorCN.GetArea().Listar2(txtArea.Text);
            string mes1,mes2, dia1,dia2;
            mes1 = bdpFecInicio.SelectedDate.Month.ToString();
            if (mes1.Length < 2)
                mes1 = "0" + mes1;
            mes2 = bdpFecFin.SelectedDate.Month.ToString();
            if (mes2.Length < 2)
                mes2 = "0" + mes2;
            dia1 = bdpFecInicio.SelectedDate.Day.ToString();
            if (dia1.Length < 2)
                dia1 = "0" + dia1;
            dia2 = bdpFecFin.SelectedDate.Day.ToString();
            if (dia2.Length < 2)
                dia2 = "0" + dia2;

            if (lst.Count == 1)
            {
              //ifrmReporte.Attributes["src"] = "http://servicios.munijesusmaria.gob.pe/appweb/appreportes/frmReportDocumentos.aspx?idArea="+ lst[0].Codigo +"&area=" + txtArea.Text + "&usuario="+ Session["usuario"] + "&fecini=" + bdpFecInicio.SelectedDate.Year + mes1 + dia1 + "&fecfin=" + bdpFecFin.SelectedDate.Year + mes2 + dia2 + txtArea.Text;
                /*
                Session.Add("pagina", "http://servicios.munijesusmaria.gob.pe/appweb/appreportes/frmItermediario.aspx?idArea=" + lst[0].Codigo + "&area=" + txtArea.Text + "&usuario=" + Session["usuario"] + "&fecini=" + bdpFecInicio.SelectedDate.Year + mes1 + dia1 + "&fecfin=" + bdpFecFin.SelectedDate.Year + mes2 + dia2 + txtArea.Text + "&tipo=1");
                AbreVentanaForm("frmNuevaVentana.aspx");
                lblErrorBusqueda.Text = "";
                 * */
                FecIni = bdpFecInicio.SelectedDate.Year + mes1 + dia1;
                FecFin = bdpFecFin.SelectedDate.Year + mes2 + dia2;
                ID = lst[0].Codigo;
                lstDocsAreaEN = MediadorCN.GetDocsArea().Listar(ID, FecIni, FecFin, txtArea.Text);
                gvAreaDocu.DataSource = lstDocsAreaEN;
                gvAreaDocu.DataBind();
            }
            else
            {
                lblErrorBusqueda.Text = "No se encontró el área especificada";
            }
        }

        protected void gvAreaDocu_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
         //   int index=0;
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {/*
                    index = e.Row.RowIndex;
                    GridViewRow row = gvAreaDocu.Rows[index-1];
                    string fecha=Server.HtmlDecode(row.Cells[1].Text);*/
                    string fecha = Server.HtmlDecode(e.Row.Cells[1].Text);
                    GridView gv = (GridView)e.Row.FindControl("gvDocumentos");
                    lstDocumentosEN = MediadorCN.GetDocumentos().Listar(ID, fecha.Substring(6, 4) + fecha.Substring(3, 2) + fecha.Substring(0, 2), fecha.Substring(6, 4) + fecha.Substring(3, 2) + fecha.Substring(0, 2));
                    gv.DataSource = lstDocumentosEN;
                    gv.DataBind();                    
                }
            }
            catch(Exception ex)
            { 
                
            }
        }

        protected void gvAreaDocu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAreaDocu.PageIndex = e.NewPageIndex;
            gvAreaDocu.DataSource = lstDocsAreaEN;
            gvAreaDocu.DataBind();
        }

        protected void gvDocumentos_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            List<DocumentosEN> lst = new List<DocumentosEN>();
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    int nro = Convert.ToInt32(Server.HtmlDecode(e.Row.Cells[0].Text));
                    GridView gv2 = (GridView)e.Row.FindControl("gvRespuesta");
                   // gv2.DataSource = objDocumentosCN.Listar(ID, fecha.Substring(6, 4) + fecha.Substring(3, 2) + fecha.Substring(0, 2), fecha.Substring(6, 4) + fecha.Substring(3, 2) + fecha.Substring(0, 2));
                    if (lstDocumentosEN[nro - 1].IdDocuRespuesta!=0)
                        lst.Add(lstDocumentosEN[nro-1]);
                    gv2.DataSource = lst;
                    gv2.DataBind();
                }
            }
            catch
            {
                
            }

        }
    }
}